<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" >
      <el-form-item label="模板名称">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模板名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        v-hasPermi="['labsafety:labsafetyCheckIndex:edit']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="labsafetyCheckIndexList" >
      <el-table-column label="编号" prop="id" width="60" align="center" />
      <el-table-column label="模板名称" >
        <template slot-scope="scope">
          <router-link :to="'/labsafety/index/detail/' + scope.row.id" class="link-type">
            <span>{{ scope.row.name }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="检查类型" align="center" prop="typeId" :formatter="typeIdFormat" width="80"/>
      <el-table-column label="适用范围"  prop="deptId" :formatter="applyScopeFormat" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="创建人" align="center" prop="userName" width="100"/>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="160">
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" v-if="isPermit('labsafety:labsafetyCheckIndex:edit')">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改安全检查指标对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="460px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板名称" prop="name">
          <el-input v-model="form.name"  placeholder="请输入内容" class="input_w"/>
        </el-form-item>
        <el-form-item label="检查类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择检查类型" class="input_w">
            <el-option
              v-for="dict in typeIdOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用范围" prop="deptId" >
           <treeselect :default-expand-level="1" v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择机构" size="small" class="input_w"/>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" rows="3"  placeholder="请输入内容" class="input_w"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPermi="['labsafety:labsafetyCheckIndex:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLabsafetyCheckIndex, getLabsafetyCheckIndex, delLabsafetyCheckIndex, addLabsafetyCheckIndex, updateLabsafetyCheckIndex, exportLabsafetyCheckIndex } from "@/api/labsafety/labsafetyCheckIndex";
import {
  treeselect,getTreeLabel
} from "@/api/labdata/orgnization";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "LabsafetyCheckIndex",
   components: { Treeselect},
  data() {
    return {
      // 部门树选项
      deptOptions: [],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 安全检查指标表格数据
      labsafetyCheckIndexList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 检查类型字典
      typeIdOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getDicts("safetyCheckOpt").then(response => {
      this.typeIdOptions = response.data;
    });
  },
  methods: {
    /** 查询安全检查指标列表 */
    getList() {
      this.loading = true;
      listLabsafetyCheckIndex(this.queryParams).then(response => {
        this.labsafetyCheckIndexList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    applyScopeFormat(row,column){
       return getTreeLabel(this.deptOptions,row.deptId);
    },
    // 检查类型字典翻译
    typeIdFormat(row, column) {
      return this.selectDictLabel(this.typeIdOptions, row.typeId);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = { };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加安全检查模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLabsafetyCheckIndex(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改安全检查模板";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLabsafetyCheckIndex(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLabsafetyCheckIndex(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除安全检查模板编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLabsafetyCheckIndex(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    isPermit(permitKey){
      if(this.$store.state.user.name=='admin'){
        return true;
      }
      let  permissions = this.$store.getters.permissions;
      return permissions.includes(permitKey);
    },
  }
};
</script>
<style type="text/css" lang="scss" scoped>
 .input_w{
   width: 300px;
 }
</style>
